//
//  NSString+MD5Addition.m
//  UIDeviceAddition
//
//  Created by Georg Kitz on 20.08.11.
//  Copyright 2011 Aurora Apps. All rights reserved.
//

#import "NSString+MD5Addition.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSString(MD5Addition)

- (NSString *) stringFromMD5{
    
    if(self == nil || [self length] == 0)
        return nil;
    
    const char *value = [self UTF8String];
    
    unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
    CC_MD5(value, strlen(value), outputBuffer);
    
    NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
        [outputString appendFormat:@"%02x",outputBuffer[count]];
    }
    
    return outputString;
}


- (NSString *)URLEncodedString
{
    NSString *result = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                           (CFStringRef)self,
                                                                           NULL,
																		   CFSTR("!*'();:@&=+$,/?%#[]"),
                                                                           kCFStringEncodingUTF8));
	return result;
    
}


- (NSString *) getFileMBSize
{
    //    int fileSize = [self intValue];
    //    float mbSize = (float)(fileSize / (1024 * 1024 * 1.0));
    //    return [NSString stringWithFormat:@"%.2fMB", mbSize];
    NSString *fileSizeStr = nil;
    int fileSize = [self intValue];
    NSString *tempSize = [NSString stringWithFormat:@"%f",fileSize/NUM/NUM];
    float size = [tempSize floatValue];
    if (size >= 1000) {
        fileSizeStr = [NSString stringWithFormat:@"%.2fGB",fileSize/NUM/NUM/NUM];
    }
    else if (size >= 0.01 && size <1000) {
        fileSizeStr = [NSString stringWithFormat:@"%.2fMB",fileSize/NUM/NUM];
    }else if (size >= 0.00001 && size < 0.01 ) {
        fileSizeStr = [NSString stringWithFormat:@"%.2fKB",fileSize/NUM];
    }else if (size < 0.00001) {
        fileSizeStr = [NSString stringWithFormat:@"%.2fB",fileSize*1.0];
    }
    return fileSizeStr;
}

@end
